define(
    ["PDUtilDir/util", "PDUtilDir/dataService", "PDUtilDir/grid", "PDUtilDir/slidebar"],
    function (Util, DataService, Grid, Slidebar) {
        function ChoiceVisitorSupportInit($scope, $compile) {
            return new ChoiceVisitorSupport($scope, $compile);
        }

        function ChoiceVisitorSupport($scope, $compile) {
            this.$scope = $scope;
            this.$compile = $compile;
        }

        ChoiceVisitorSupport.fn = ChoiceVisitorSupport.prototype;
        ChoiceVisitorSupport.fn.init = function (index, oldId) {
            this.entity = {
                index: index,
                oldId: oldId,
                visitor: undefined,
            };
            this.saerch = {};
        }
        ChoiceVisitorSupport.fn.digest = function () {
            this.$scope.$digest();
        }
        ChoiceVisitorSupport.fn.run = function (index, oldId) {
            var _this = this;
            _this.init(index, oldId);
            _this.slidebars = Slidebar({
                size: 'lg',
                url: getServer() + "/static/app/order/orderDoneInfos/views/replaceVisitor.html",
//					close : false,
                $scope: this.$scope,
                $compile: this.$compile,
                afterLoad: function () {
                    _this.initialization();
                    _this.digest();
                }
            });
        };
        ChoiceVisitorSupport.fn.initialization = function () {
            var _this = this;
            _this.grid = Grid({
                id: "VisitorForSearchGrid",
                multi: false,
                pagination: true,
                index: true,
                cache: false,
                onSeletedChanged: function (data) {
                    var len = data.length;
                    if (len == 0) {
                        _this.entity.visitor = undefined;
                        _this.$scope.addVisitorForm.visitorIds.$setViewValue(undefined);
                        return;
                    }
                    _this.$scope.addVisitorForm.visitorIds.$setViewValue(true);
                    _this.entity.visitor = data[0];
                },
                layout: [{
                    name: "中文名",
                    field: "zhName",
                    width: "60px",
                    sort: true
                }, {
                    name: "英文名",
                    field: "enName",
                    width: "60px",
                    sort: true
                }, {
                    name: "性别",
                    field: "sex",
                    width: "40px",
                    format: function (e) {
                        if (e.row.sex == "1" || e.row.sex == 1)
                            return '男';
                        else
                            return '女';
                    }
                }, {
                    name: "年龄",
                    width: "40px",
                    field: "age"
                }, {
                    name: "联系电话",
                    width: "80px",
                    field: "phone"
                }, {
                    name: "证件类型",
                    field: "certificates",
                    width: "60px",
                    format: function (e) {
                        return _this.$scope.cache.certificatesObject.get(e.row.certificates);
                    }
                }, {
                    name: "证件号码",
                    field: "certificatesNumber"
                }],
                toolbar: [{
                    name: "查询",
                    icon: "fa fa-search",
                    callback: function (event) {
                        _this.searchFun();
                    }
                }],
                data: {
                    type: 'URL',
                    value: getServer() + '/flea/hclv/visitor/selectByDept.htmls'
                },
                onLoadDataSuccess: function (data) {
                    var selecteds = _this.$scope.detailSupport.entity.visitors,
                        len = selecteds.length,
                        i = 0;

                    if (len == 0)
                        return data;

                    for (var n = 0; n < data.length; n++) {
                        for (i = 0; i < len; i++) {
                            if (data[n] && data[n].id == selecteds[i].id) {
                                data.splice(n, 1);
                            }
                        }
                    }
                    return data;
                }
            });
        };
        ChoiceVisitorSupport.fn.searchFun = function () {
            this.grid.reload({
                queryParam: this.search
            })
        };
        ChoiceVisitorSupport.fn.save = function () {
            var _this = this;
            Util.confirm("是否要更换", function () {
                DataService.setObject('order.orderInfo.visitor.replace', _this.entity);
                _this.$scope.detailSupport.replaceFun();
                _this.slidebars.close();
            });
        };
        return ChoiceVisitorSupportInit;
    });